package com.cskaoyan.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

@Configuration
@ComponentScan(value = "com.cskaoyan",
        excludeFilters =@ComponentScan.Filter({Controller.class, EnableWebMvc.class}))
@EnableTransactionManagement
public class RootConfiguration  {
    @Bean
      public DataSource dataSource(){
                DruidDataSource dataSource = new DruidDataSource();
                dataSource.setDriverClassName("com.mysql.jdbc.Driver");
                dataSource.setUrl("jdbc:mysql://localhost:3306/45th?useUnicode=true&characterEncoding=utf-8");
                dataSource.setUsername("root");
                dataSource.setPassword("123456");
                return dataSource;
            }
            @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
                SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
                sqlSessionFactoryBean.setDataSource(dataSource);
                return sqlSessionFactoryBean;
            }
            @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
                MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
                mapperScannerConfigurer.setBasePackage("com.cskaoyan.mapper");
                mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
             return mapperScannerConfigurer;
    }
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
